package com.itheima.mp.service.impl;

import cn.hutool.db.Page;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mp.Query.pageQuery;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.dto.Pagedto;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.Iuserservice;
import com.itheima.mp.vo.Uservo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//现在就是关于注解的问题
@RequiredArgsConstructor
public class Iuserserviceimpl extends ServiceImpl<UserMapper,User> implements Iuserservice {
   // @Autowired
    private final UserMapper userMapper;
    /*需求
    * 需求:实现一个根据复杂条件查询用户的接口，查询条件如下
             name:用户名关键字，可以为空
             status:用户状态，可以为空
            minBalance:最小余额，可以为空
             maxBalance:最大余额，可以为空
    * */
    //现在这个就是多条语句查询

   /*
    public List<User> selectbyconditions(String name, Integer status, Integer minBalance, Integer maxBalance) {

//利用mybatis-plus
        final LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(name!=null&name!=""){
            lambdaQueryWrapper.like(User::getUsername,name);

        }
        if(status!=null){
            lambdaQueryWrapper.eq(User::getStatus,status);
        }
        if(minBalance!=null){
            lambdaQueryWrapper.ge(User::getBalance,minBalance);

        }
        if(maxBalance!=null){
            lambdaQueryWrapper.ge(User::getBalance,maxBalance);

        }


        return userMapper.selectList(lambdaQueryWrapper);
    }*/




    @Override
    public List<User> selectbyconditionsdemo(String name, Integer status, Integer minBalance, Integer maxBalance) {

//利用mybatis-plus
        final LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(name!=null&name!=""){
            lambdaQueryWrapper.like(User::getUsername,name);

        }
        if(status!=null){
            lambdaQueryWrapper.eq(User::getStatus,status);
        }
        if(minBalance!=null){
            lambdaQueryWrapper.ge(User::getBalance,minBalance);

        }
        if(maxBalance!=null){
            lambdaQueryWrapper.ge(User::getBalance,maxBalance);

        }
        final List<User> list = this.lambdaQuery().like(User::getUsername, name)
                .eq(name != null & name != "", User::getStatus, status).
                ge(status != null, User::getBalance, minBalance)
                .ge(maxBalance != null, User::getBalance, maxBalance)
                .ge(minBalance != null, User::getBalance, minBalance)
                //在这里就是说展示一行还是一个集合
                //  .one()
                .list();


        return list;
    }

    @Override
    public Uservo<Pagedto> selectpagehelp1(pageQuery pageQuery1) {
      //拼接分页条件
         Page page = new Page(pageQuery1.getPageno(), pageQuery1.getPagesize());
        if(pageQuery1.getSortby()!=null){
           // page.addOrder(new OrderItem(pageQuery1.getSortby(), pageQuery1.isAsc()));
            page.addOrder(new OrderItem(pageQuery1.getSortby(),pageQuery1.isAsc()));
        }else{
            //默认按照更新时间尽心排序
            page.addOrder(new OrderItem("update_time",false));
        }
//分页查询
        Page page1 = this.page(page);
        //封装结果集
        final Pagedto<Object> pagedto = new Pagedto<>();
        pagedto.setTotal(page1.getTotal());
        pagedto.setPages(page1.getPages());
       List<pagedto> userdto  = page1.getRecords();
       
        return null;
    }


}
